<?php


namespace App\Http\Services\Admin;


use App\Http\Constants\Account\CommonCoinCoinConstants;
use App\Http\Constants\Account\CommonCoinCoinLogConstants;
use App\Http\Constants\CoinsExceptionConstants;
use App\Models\CoinsExceptionRecord;
use YiluTech\ComplexSearch\ComplexSearch;

class CoinsExceptionRecordService extends ComplexSearch
{
    public $root = CoinsExceptionRecord::class;
    protected $display = 'simple';
    protected $orderBy = 'id desc';
    protected $joins = ['member'];

    protected $username = null;

    protected $headers = [
        [
            ['value' => 'ordersn', 'label' => '订单编号'],
            ['value' => 'member_id', 'label' => '会员ID'],
            ['value' => 'nickname', 'label' => '会员昵称'],
            ['value' => 'username', 'label' => '会员手机号'],
            ['value' => 'coins_type_name', 'label' => '明细类型'],
            ['value' => 'account_type', 'label' => '账户类型'],
            ['value' => 'change_amount', 'label' => '红包变动'],
            ['value' => 'exception_cause', 'label' => '异常信息'],
            ['value' => 'created_at', 'label' => '创建时间'],
            ['value' => 'update_at', 'label' => '修改时间'],
            ['value' => 'remark', 'label' => '备注'],
            ['value' => 'status', 'label' => '状态'],
        ]
    ];

    protected $conditions = [
        'ordersn' => [
            'ctype' => 'input',
            'operator' => '=',
            'label' => '订单编号',
        ],
        'member_id' => [
            'ctype' => 'input',
            'operator' => '=',
            'label' => '会员ID',
        ],
        'username' => [
            'ctype' => 'input',
            'operator' => '=',
            'label' => '会员手机号',
            'custom' => true,
        ],
        'account_type' => [
            'ctype' => 'select',
            'operator' => CommonCoinCoinConstants::COINS_ACCOUNT_TYPE,
            'label' => '账户类型',
        ],
        'coins_type' => [
            'ctype' => 'select',
            'operator' => CommonCoinCoinLogConstants::COIN_ACTION,
            'label' => '明细类型',
            'custom' => true,
        ],
        'created_at' => [
            'ctype' => 'date-in',
            'format' => 'Y-m-d',
            'display' => true,
            'label' => '创建时间',
        ],
        'status' => [
            'ctype' => 'select',
            'operator' => CoinsExceptionConstants::STATUS_TYPE,
            'label' => '状态',
        ],
    ];

    public function make($request)
    {
        if ($request['action'] == "query") {
            foreach ($request['params'] as $k => $v) {
                if ($v[0] == 'username') {
                    $this->username = $v[2];
                    unset($request['params'][$k]);
                }

                if ($v[0] == 'coins_type_name') {
                    $v[0] = 'coins_type';
                    $request['params'][$k] = $v;
                }

            }
        }

        return parent::make($request);
    }

    public function get()
    {
        $result = parent::get();
        if ($this->action !== 'prepare') {
            $result = $result->toArray();
            if (!empty($result['data'])) {
                foreach ($result['data'] as &$v) {
                    $v['coins_type_name'] = $v['coins_type'];
                    unset($v['coins_type']);
                    $member = \SharedCache::getMember($v['member_id']);
                    $v['username'] = $member['username'] ?: '';
                    $v['nickname'] = $member['nickname'] ?: '';
                }
                $result = CommonService::objRegroupArr($result);
            }
        }
        return $result;

    }

    public function query()
    {
        $select = ['id', 'ordersn', 'member_id', 'coins_type', 'account_type', 'change_amount', 'exception_cause', 'created_at', 'remark', 'status'];
        $query = parent::query();
        $query->select($select);
        if (!is_null($this->username)) {
            $member = \SharedCache::memberInfoByPhone($this->username);
            if (!empty($member['id'])) $query->where('member_id', $member['id']);
        }

        return $query;
    }

    public function detail($id)
    {
        $select = [
            'id',
            'member_id',
            'coins_type',
            'change_amount',
            'exception_cause',
            'created_at',
            'status',
            'ordersn',
            'remark',
            'auditor',
            'auditor_at',
            'execute',
            'execute_at',
            'operate',
            'operate_at',
        ];
        $result = CoinsExceptionRecord::select($select)->find($id);
        $result = $result->toArray();
        $member = \SharedCache::getMember($result['member_id']);
        $result['username'] = $member['username'];
        $result['nickname'] = $member['nickname'];
        return $result;
    }

}
